package borge.web.teamspirit.client.widgets;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.servercomm.ServerComm;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MenuWidget extends BaseWidget {

	private Tree iMenuTree;
	private VerticalPanel iPanel;
	//private String iLastClickedItem; // TEMP SOLUTION
	
	// 4 menu trees? general, sport related, personal, admin
	// some menus only shown if logged in
	
	private String[] iMenuItems = {
			"News",
			"The team",
			"Forum",
			"Search",
			"Statistics",
			"Series",
			"TestCreate",
			"TestPrint"
			
	};
	
	private String[] iAdminItems = {
			TeamSpirit.getConstants().create_new_team(),
			TeamSpirit.getConstants().create_new_user(),
	        TeamSpirit.getConstants().create_new_player(),
	        TeamSpirit.getConstants().create_new_serie(),
	        TeamSpirit.getConstants().create_new_match(),
	        TeamSpirit.getConstants().list_teams(),
	        TeamSpirit.getConstants().list_users(),
	        TeamSpirit.getConstants().list_players(),
	        TeamSpirit.getConstants().list_series(),
	        TeamSpirit.getConstants().list_matches()
	};
	
	private String[] iPersonalItems = {
			"My profile",
			"My settings",
			"My statistics"
	};
	
	private String iMenuName = TeamSpirit.getConstants().menu();
	private String iAdminName = "Admin";
	private String iPersonalName = "My stuff";
	private String iLastClickedItem = "";
	
	public MenuWidget(WidgetEventHandler aEventHandler){
		
	    super(aEventHandler);
	    
		iMenuTree = new Tree();
		TreeItem menuItem = iMenuTree.addItem(iMenuName);
		TreeItem adminItem = iMenuTree.addItem(iAdminName);
		TreeItem personalItem = iMenuTree.addItem(iPersonalName);
		
		for(int i = 0; i < iMenuItems.length; ++i){
			menuItem.addItem(iMenuItems[i]);
		}
		
		for(int i = 0; i < iAdminItems.length; ++i){
			adminItem.addItem(iAdminItems[i]);
		}
		
		for(int i = 0; i < iPersonalItems.length; ++i){
			personalItem.addItem(iPersonalItems[i]);
		}
		
		menuItem.setState(true);
		adminItem.setState(true);
		personalItem.setState(true);
		
		iPanel = new VerticalPanel();
		iPanel.add(iMenuTree);
		iMenuTree.addSelectionHandler(new MySelectionHandler());
		initWidget(iPanel);
		
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
		      public void onValueChange(ValueChangeEvent<String> event) {
		        String historyToken = event.getValue();
		        iLastClickedItem = event.getValue();
		        //postEvent(WidgetEventType.MENU_CLICKED);
		      }
		    });

		
	}
	
	public String getClickedLinkText(){
	    return iLastClickedItem;
	}
	
	public void clearClickHistory(){
	    iLastClickedItem = "";
	}
	
	private class MySelectionHandler implements SelectionHandler<TreeItem> {
		
		public void onSelection(SelectionEvent<TreeItem> event){
		 
			//Window.alert("selected");
			if(event.getSelectedItem().getText().equals("TestCreate")){
				ServerComm.testCreate();
				return;
			}
			else if(event.getSelectedItem().getText().equals("TestPrint")){
				ServerComm.testPrint();
				return;
			}
			
		    // TODO:
		    // create a class menu to represent menu data
		    // let each tree item map to the widget that should be displayed
		    
			//TreeItem item = event.getSelectedItem();
			//Window.alert("Selected: " + item.getText());
			
			// TEMP SOLUTION
			if(!iLastClickedItem.equals(event.getSelectedItem().getText())){
				iLastClickedItem = event.getSelectedItem().getText();
				History.newItem(event.getSelectedItem().getText());
				postEvent(WidgetEventType.MENU_CLICKED);
			}
		}
		
	}
	
}
